//
//  CPageController.swift
//  WanZhou
//
//  Created by xhl_dabaoMac on 2021/11/15.
//

import UIKit

public class CPageController: UIViewController {

    public var titleViews : [UIView]?

    public var titleScrollHeight = 44 //顶部的高度
    public var lastIndex = 0 //上次选中的的索引，默认0

    public var controllers : [UIViewController]?
    public var location = CLocation.left
    public static var BGColor = UIColor.white
    public let titleScrollView : UIScrollView = {
        
        let scrollView = UIScrollView()
        scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false;
        scrollView.bounces = false
        scrollView.backgroundColor = CPageController.BGColor
        return scrollView
        
    }()

    public let bottomLineView : UIImageView = {
        
        let imageView = UIImageView()
        imageView.backgroundColor = CColor.theme
        imageView.layer.cornerRadius = 1.5
        imageView.layer.masksToBounds = true
        return imageView
        
    }()
    let scv = ScrollControllerView(frame: .zero)
    

    public override func viewDidLoad() {
        super.viewDidLoad()
        addTitleScrollView()
        addControlls()
        
        setDefaultSelectIndex()
    }
}

extension CPageController{
    
    ///   添加头部
    func addTitleScrollView() {
        
        view.addSubview(titleScrollView)
        titleScrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(titleScrollHeight)
        }
        guard let views = titleViews else {return}
        titleScrollView.layoutMultipleView(startlocation: .left,views: views, sideSpace:10,itemSpace: 10) {
            [weak self] index in
            guard let self = self else { return }
            let difference  = abs(index - self.lastIndex)
                                  
            self.scv.setSelectIndex(index: index,animated:difference > 1 ? false:true)
            self.selectTitle(index: index)
        }
    }
    
    /// 设置选中头部第几个人
    /// - Parameter index: 选择索引
    func selectTitle(index :Int)  {
        guard let views = titleViews else {return}
        let view = views[index]
        bottomLineView.removeFromSuperview()
        titleScrollView.addSubview(bottomLineView)
        UIView.animate(withDuration: 0.75) { [self] in
            self.bottomLineView.snp.makeConstraints { make in
                make.bottom.equalToSuperview().offset(-1)
                make.width.equalTo(25);
                make.height.equalTo(3);
                make.centerX.equalTo(view.snp.centerX)
            }
            titleScrollView.scrollToCenter(tapSubView: view)
        }
        updateTitleScrollView(index: index)
    }
    
    /// 添加控制器
    func addControlls() {
       
        guard let cons = controllers else {return}
        for con in cons {
            addChild(con)
        }
        
        scv.controllers = controllers
        scv.selectIndexAction = {
            [weak self] index in
            self?.selectTitle(index: index)
        }
        view.addSubview(scv)
        scv.snp.makeConstraints { make in
            make.top.equalTo(titleScrollView.snp.bottom)
            make.bottom.left.right.equalToSuperview()
        }
        
    }
    
    /// 获取添加view之后的contentSzie
    public override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        guard let views = titleViews else {return}
        
        let lastView = views.last
        
        DispatchQueue.global().asyncAfter(deadline: .now()+0.1) {
            DispatchQueue.main.async {
                self.titleScrollView.contentSize = CGSize(width: lastView?.frame.maxX ?? 0, height: 0)
            }
        }
    }
    
    
    /// 设置默认选中的索引
    /// - Parameter index: 默认位置
    public func setDefaultSelectIndex(index:Int = 0){
 
        updateTitleScrollView(index: index)
        selectTitle(index: index)
        scv.setSelectIndex(index: index)
        
    }
    
    /// 更新头部选中的样式
    /// - Parameter index: 选中的index
    public func updateTitleScrollView(index:Int = 0){
        guard let views = titleViews else {return}
        
        let lastView = views[lastIndex]
        if lastView.isKind(of: UILabel.self) {
            let label = lastView as! UILabel
            label.textColor = CColor.c33
            label.font = UIFont.systemFont(ofSize: 15)
        }
        
        let view = views[index]
        if view.isKind(of: UILabel.self) {
            let label = view as! UILabel
            label.textColor = CColor.theme
            label.font = UIFont.systemFont(ofSize: 17)

        }
        lastIndex = index
    }
    
    
}
